#include "qcommondelegate.h"

#include "qattributeeditor.h"

#include "../shared/qdriveattribute.h"
#include "../shared/qdrivevaluemap.h"

#include <QApplication>
#include <QPainter>
#include <QHeaderView>
#include <QLineEdit>
#include <QComboBox>

QCommonDelegate::QCommonDelegate():
    m_creator(new QEditorCreator)
{

}


void QCommonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV3 opt=option;

    QColor c;
    c=QColor(255,230,191);
    if(opt.features & QStyleOptionViewItemV2::Alternate)
    {
        c=c.lighter(112);
    }
    painter->fillRect(option.rect,c);

    opt.state &=~ QStyle::State_HasFocus;
    QItemDelegate::paint(painter,opt,index);
    opt.palette.setCurrentColorGroup(QPalette::Active);
    QColor color=static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor,&opt));
    painter->setPen(color);
    painter->drawLine(opt.rect.bottomLeft(),opt.rect.bottomRight());

    if(index.column()<index.model()->columnCount(index)-1)
    {
        int right=option.direction==Qt::LeftToRight?option.rect.right():option.rect.left();
        painter->drawLine(right,option.rect.y(),right,option.rect.bottom());
    }
}

QWidget *QCommonDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QWidget* wid=NULL;
    QDriveAttribute *attr=(QDriveAttribute*)(index.data(Qt::UserRole+0x1000).toInt());
    QDriveValueMap  *value=(QDriveValueMap*)(index.data(Qt::UserRole+0x1001).toInt());
    if(attr!=NULL && attr->attribute("isEdited",true).toBool())
    {
        wid=m_creator->createAttributeEditor(attr,parent);
    }
    else if(value!=NULL && value->header(index.column()-1)->property("isEdited").toBool())
    {
        wid=m_creator->createValueEditor(value,parent,index.row(),index.column()-1);
    }

    return wid;
}

QSize QCommonDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize sz=QItemDelegate::sizeHint(option,index);
    sz.setHeight(20);
    return sz;
}

void QCommonDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect.adjusted(0,0,0,-1));
}

QCommonTreeWidget::QCommonTreeWidget(QWidget *parent):
    QTreeWidget(parent)
{
    header()->setVisible(false);
    setItemDelegate(new QCommonDelegate);
    setAlternatingRowColors(true);

    setFrameStyle(QFrame::NoFrame);
    header()->setMovable(false);
}

void QCommonTreeWidget::drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const
{
    QStyleOptionViewItemV3 opt=options;
    QColor c;
    c=QColor(255,230,191);
    opt.palette.setColor(QPalette::AlternateBase,c.lighter(112));
    painter->fillRect(opt.rect,c);
    opt.state &=~ QStyle::State_HasFocus;
    QTreeWidget::drawRow(painter,opt,index);

    opt.palette.setCurrentColorGroup(QPalette::Active);
    QColor color=static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor,&opt));
    painter->setPen(color);
    painter->drawLine(opt.rect.bottomLeft(),opt.rect.bottomRight());
}
